// this is the texture map data we can sample
uniform sampler2D textureMap, heightMap, normalMap;

// these are the texture and bump mapping settings we can toggle
uniform bool bumpMapEnabled, textureMapEnabled, phongEnabled;

// These varying values are interpolated over the surface
varying vec4 v;
varying vec3 t;
varying vec3 b;
varying vec3 n;

void main()
{
    // sample from a texture map
    vec4 texcolor;
    if (textureMapEnabled) {
        texcolor = texture2D(textureMap,gl_TexCoord[0].st);
    } else {
        texcolor = vec4(1,1,1,1);
    }
    // HINT: you can also sample from the height map for parallax bump mapping (extra credit option)
    
    // sample from a normal map
    vec3 normal;
    if (bumpMapEnabled) {
        normal = (vec3(texture2D(normalMap, gl_TexCoord[0].st)) * 2);
        normal[0] -= 1;
        normal[1] -= 1;
        normal[2] -= 1;
    } else {
        normal = vec3(0,0,1);
    }
    
    // @TODO: transform normal in to eye space
    // HINT: also use the normalize() function to normalize things
    // You may also want something like "if (phongEnabled) { etc" here-ish
    if (phongEnabled) {
        mat3 tbn = mat3(t, b, n);
        normal = normalize(tbn * normal);
    }
    
    if (!phongEnabled) {
      normal = n;
    }
    // normalize the eye, light, and reflection vectors
    vec3 e = normalize(v).xyz; // in eye space, eye is at origin
    // light sources are in eye space; it is okay to assume directional light
    vec3 l = normalize(gl_LightSource[0].position.xyz);
    vec3 r = reflect(-l,normal); // reflect function assumes vectors are normalized
    
    // setting up your light parameters in the shader is fine
    // don't worry about matching the opengl lighting settings
    // the fixed function uses the blinn-phong half-angle formula anyway
    // while we've asked you to use the standard phong model
    float p = 20.0;
    vec4 cr = texcolor;
    vec4 cl = gl_LightSource[0].diffuse;
    vec4 ca = vec4(.2,.2,.2,1.0);
    
    // the below is fine for a lighting equation
    vec4 color;
    color = cr * (ca + cl * max(0.0,dot(normal,l))) + 
                    cl * pow(max(0.0,dot(r,normal)),p);
  
    // set the output color to what we've computed
    gl_FragColor = color;
}
